package com.hgk.wanwan.login.forgetPas;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.common.android.volley.VolleyError;
import com.common.dongqiuhui.apps.comm.constants.UrlConstans;
import com.common.dongqiuhui.apps.comm.model.BaseController;
import com.hgk.common.common.ui.activity.SimpleBaseActivity;
import com.hgk.common.common.ui.view.RippleView;

import java.util.Map;

import width.hgk.wanwan.R;

public class ForgetPsdActivity extends SimpleBaseActivity  {

    private final String GET_CODE_FORGET_URL = UrlConstans.ZWCOMMOMURL+"/domain/user/check_smscode.json";
    private EditText mPhoneNumber, mCodeWord;
    private RippleView mRipNext,mGetCode;
    private Button mBtnNext;
    private ImageView mDelePhone,mDeleCode;
    private String mPhNumber,mCode;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_forget_psd);
        initView();
    }

    private void initView() {
        mPhoneNumber = (EditText) findViewById(R.id.tv_regist_phone_number_forget);//输入手机号
        mCodeWord = (EditText) findViewById(R.id.tv_regist_codenum_code_forget);//输入验证码

        mDeleCode = (ImageView) findViewById(R.id.iv_regist_code_delete);
        mDelePhone = (ImageView) findViewById(R.id.iv_phone_numb_delete);
        mGetCode = (RippleView) findViewById(R.id.rip_get_security_code);
        mRipNext = (RippleView) findViewById(R.id.rip_next_step_btn_forget);
        mBtnNext = (Button) findViewById(R.id.btn_next_step_btn_forget);


        mCodeWord.addTextChangedListener(new Watcher(mCodeWord));
        mPhoneNumber.addTextChangedListener(new Watcher(mPhoneNumber));

        mGetCode.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
            @Override
            public void onComplete(RippleView rippleView) {
                if (TextUtils.isEmpty(mPhoneNumber.getText().toString())){
                    Toast.makeText(mContext,"手机号不能为空",Toast.LENGTH_SHORT).show();
                }else {
                    getNetDataForPost("get_code", GET_CODE_FORGET_URL, BaseController.class);
                }
                }
        });
        mRipNext.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
            @Override
            public void onComplete(RippleView rippleView) {
//                if (!TextUtils.isEmpty(mCode) && !TextUtils.isEmpty(mPhNumber)){
                    PwdRetakeActivity.launch(mContext, mCode, mPhNumber);
//                }else {
//                    Toast.makeText(mContext,"手机号码与验证码均不能为空",Toast.LENGTH_SHORT).show();
//                }
            }
        });
    }

    @Override
    protected Map getApiParams(String action) {
        return null;
    }

    @Override
    protected void success(String action, Object result) {

    }

    @Override
    protected void fail(String action, String errorStr) {

    }

    @Override
    protected void netfail(String action, VolleyError volleyError) {

    }

    @Override
    public void setLeftButton(RelativeLayout relativeLayout, Button button) {

    }

    @Override
    public void setRightButton(RelativeLayout relativeLayout, Button button) {

    }

    @Override
    public Object getTitleString() {
        return "找回密码";
    }

    @Override
    public int getBodyView() {
        return R.layout.activity_forget_psd;
    }

    public static void launch(Context context) {
        context.startActivity(new Intent(context, ForgetPsdActivity.class));
    }


    public class Watcher implements TextWatcher{
        private EditText EditId = null;
        public Watcher(EditText etId){
            EditId = etId;
        }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (EditId == mPhoneNumber){
                if (s.length()>0){
                    mDelePhone.setVisibility(View.VISIBLE);
                }else{
                    mDelePhone.setVisibility(View.GONE);
                }
            }else if(EditId == mCodeWord){
                if (s.length()>0){
                    mDeleCode.setVisibility(View.VISIBLE);
                }else{
                    mDeleCode.setVisibility(View.GONE);
                }
            }
            if (!TextUtils.isEmpty(mPhoneNumber.getText().toString())&&!TextUtils.isEmpty(mCodeWord.getText().toString())){
                mBtnNext.setBackgroundResource(R.drawable.btn_bg);
                mBtnNext.setEnabled(true);
                mPhNumber = mPhoneNumber.getText().toString();
                mCode = mCodeWord.getText().toString();
            }
        }
        @Override
        public void afterTextChanged(Editable s) {

        }
    }
}
